/*
 * BizErrorHelper.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.net.exception;

import android.text.TextUtils;

import com.yidiandao.business.constant.ServerCode;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.utils.BuglyUtils;
import com.yidiandao.business.utils.ExceptionUtils;
import com.yidiandao.business.utils.LogUtils;

/** 网络业务错误帮助类. */
public class BizErrorHelper {

    /**
     * 解析服务端返回的错误信息并封装为BaseResponseType.
     *
     * @param baseResponse 服务端响应的内容
     * @return 业务错误信息
     */
    public static BizErrorInfo generateBizErrorInfo(BaseResponse baseResponse) {

        BizErrorInfo bizErrorInfo;
        int code = baseResponse.getCode();
        String message = baseResponse.getMsg();

        /*        if (code == ServerCode.FAILED) {
            // 接口访问失败,一般是缺少参数引起
            message = baseResponse.getMsg();
        } else if (code == ServerCode.INVALID_TOKEN) {
            // Token失效
            message = baseResponse.getMsg();
        } else if (code == ServerCode.FAILED_MESSAGE) {
            // 接口访问失败，有错误提示
            message = baseResponse.getMsg();
        } else if (code == ServerCode.SERVER_ERROR) {
            // 服务器出错了
            message = baseResponse.getMsg();
        } else {
            // 其他错误
            message = baseResponse.getMsg();
        }*/

        bizErrorInfo = new BizErrorInfo(code, message, baseResponse);
        return bizErrorInfo;
    }

    /**
     * 报告业务错误, 在前台打印日志信息, 远程通过友盟上传错误日志.
     *
     * @param url 地址
     * @param request 请求信息
     * @param bizErrorInfo 错误信息
     */
    public static void reportError(
            final Class clazz,
            final String url,
            final BaseRequest request,
            final BizErrorInfo bizErrorInfo) {
        if (clazz == null || TextUtils.isEmpty(url) || request == null || bizErrorInfo == null) {
            return;
        }
        int code = bizErrorInfo.getCode();
        String content =
                "错误代码 : "
                        + code
                        + "\nurl : "
                        + url
                        + "\n请求参数 : "
                        + request.toJson()
                        + "\n响应结果 : "
                        + bizErrorInfo.getResponse().toJson();

        if (code == ServerCode.FAILED || code == ServerCode.SERVER_ERROR) {
            BusinessException businessException = new BusinessException();
            ExceptionUtils.addMessage(
                    businessException,
                    clazz,
                    "Response-Result : " + bizErrorInfo.getResponse().toJson());
            ExceptionUtils.addMessage(
                    businessException, clazz, "Request-Params : " + request.toJson());
            ExceptionUtils.addMessage(
                    businessException,
                    clazz,
                    "Server-ParserError, code : " + code + ", url : " + url);

            // 上报错误
            BuglyUtils.reportError(businessException);
        }

        // 前台打印信息
        LogUtils.e(content);
    }
}
